#include "alarmFormV1.h"
#include "ui_alarmFormV1.h"
#include "MemoGlobal.h"

AlarmFormV1::AlarmFormV1(QSqlDatabase _db,
                         QWidget *parent)
    :m_SqlDB(_db),
    QWidget(parent),
    ui(new Ui::AlarmFormV1)
{
    ui->setupUi(this);
    initUIs();
    updateAlarms();

    m_pTimer = new QTimer(this);
    connect(m_pTimer, SIGNAL(timeout()),
            this, SLOT(checkAlarm()));
    m_pTimer->start(1000);
}

AlarmFormV1::~AlarmFormV1()
{
    delete ui;
}

void AlarmFormV1::changeEvent(QEvent *e)
{
    QWidget::changeEvent(e);
    switch (e->type()) {
    case QEvent::LanguageChange:
        ui->retranslateUi(this);
        break;
    default:
        break;
    }
}

void AlarmFormV1::updateAlarms()//query db again
{
    m_pAlarmRecoedModel->setTable("AlarmMemoList");
    m_pAlarmRecoedModel->setSort(1,Qt::AscendingOrder);
    m_pAlarmRecoedModel->select();

    m_pAlarmQueryModel->setQuery("SELECT AlarmID, AlarmDate FROM AlarmMemoList ORDER BY AlarmDate ASC",this->m_SqlDB);

    //copy data to alarm list
    m_AlarmList.clear();
    for(int i=0;i<m_pAlarmQueryModel->rowCount();i++){
        AlarmStruct _newAlarm;
        _newAlarm.strID = m_pAlarmQueryModel->record(i).value("AlarmID").toString();
        _newAlarm.dtAlarmDate = QDateTime::fromString(m_pAlarmQueryModel->record(i).value("AlarmDate").toString().left(16),Memo::strDateTimeConvertFormat);
        m_AlarmList.append(_newAlarm);
    }

    m_AlarmIndex = 0;
}

void AlarmFormV1::initUIs()
{
    m_pAlarmRecoedModel = new QSqlTableModel(this, this->m_SqlDB);
    m_pAlarmQueryModel = new QSqlQueryModel;


}

void AlarmFormV1::checkAlarm()
{

}



